package com.we823.project.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jquery.jqgrid.model.JqGrid;
import com.we823.project.model.BaseModel;
import com.we823.project.repository.service.BaseService;
import com.we823.utils.web.RequestUtil;

public abstract class AbstractSimpleBaseController<T extends BaseModel,ID> {
	protected String baseUrl = "";
	private BaseService<T, ID> baseService;
	
	protected PermissionList permissionList = null;

	public AbstractSimpleBaseController(BaseService<T, ID> baseService) {
		this.baseService = baseService;
	}
	
	@RequestMapping("list")
	public String list(){
		if (permissionList!=null) {
			this.permissionList.assertHasViewPermission();
		}
		return baseUrl + "list";
	}
	
	@RequestMapping("getJson")
	@ResponseBody
	public Model getJson(HttpServletRequest request,Model model){
		if (permissionList!=null) {
			this.permissionList.assertHasViewPermission();
		}
		boolean pageable = RequestUtil.getPageable(request);
		JqGrid<T> jqgridModel = new JqGrid<>(request, model);
		PageRequest pageRequest = new PageRequest(jqgridModel.getPage()-1, jqgridModel.getRows(), getSort(request));
		
		Page<T> dataPage = getData(pageRequest, request, model, pageable);
		
		if(dataPage!=null){
			jqgridModel.setDatarows(dataPage.getContent());
			jqgridModel.setRecords(dataPage.getTotalElements());
		}
		jqgridModel.flush();
		return model;
	}
	
	protected Sort getSort(HttpServletRequest request) {
		return new Sort(Direction.ASC,"id");
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("process")
	@ResponseBody
	public Model process(HttpServletRequest request,Model model){
		String oper = RequestUtil.getValueByParam(request, "oper","");
		switch (oper) {
		case "add":{
			if (permissionList!=null) {
				permissionList.assertHasCreatePermission();
			}
			T entity = getEntity(request,null,oper);
			baseService.save(entity);
			break;
		}
		case "edit":{
			if (permissionList!=null) {
				permissionList.assertHasEditPermission();
			}
			
			int id = RequestUtil.getValueByParam(request, "id",-1);
			T entity = getEntity(request,id,oper);
			if(id!=-1){
				baseService.update(entity);
			}
			break;
		}
		case "del":{
			if (permissionList!=null) {
				permissionList.assertHasDeletePermission();
			}
			ID id = (ID)RequestUtil.getValueByParam(request, "id", 0);
			if(id != null){
				boolean canDelete = checkDelete(id);
				if(canDelete) {
				   baseService.delete(id);	
				}else {
					model.addAttribute("hasError",true);
					model.addAttribute("message","无法删除此信息");
				}
			}
			break;
		}
		default:
			break;
		}
		return model;
	}

	/**
     * 权限前缀：如sys:user
     * 则生成的新增权限为 sys:user:create
     */
    public void setResourceIdentity(String resourceIdentity) {
        if (!StringUtils.isEmpty(resourceIdentity)) {
            permissionList = PermissionList.newPermissionList(resourceIdentity);
        }
    }
    
    private boolean checkDelete(ID id) {
		return true;
		
	}
    
	/**
	 * 
	 * @param pageRequest
	 * @param request
	 * @param model
	 * @param pageable
	 * @return
	 */
	public abstract Page<T> getData(PageRequest pageRequest,HttpServletRequest request,Model model,boolean pageable);
	/**
	 * 根据request获取Entity，当ID不为null时，需要赋值给ID
	 * @param request
	 * @param id
	 * @return
	 */
	public abstract T getEntity(HttpServletRequest request,Integer id,String oper);
}
